from django.shortcuts import render
from django.http import HttpResponse, HttpResponseRedirect
from django.urls import reverse

from App.mod import AuthPermission


# Create your views here.
def index(request):
    return HttpResponse('ようこそ')


def home(request):
    # users=User.objects.all()

    users = AuthPermission.objects.all()
    return render(request, "index.html", context={'message': 'Have a nice day!!', 'users': users})


def showInt(request, num, name, some_path='___'):
    return_dict = {'number': str(num), 'name': str(name), 'some_path': str(some_path)}
    # three kinds of augments:number, slug and path
    print(str(return_dict))
    return render(request, "int.html", context=return_dict)


def mainpage(request):
    return render(request, 'main.html')


def showRe(request, mess):
    print(request.GET)
    # print(request.GET.dict())
    print(request.GET.get('b'))
    # /zi/re/1111/?a=1&b=2&a=3
    print(request.GET.getlist('a'))
    # 获取多个值应该用getList

    # print(request.path)
    # 请求路径

    # print(request.META)

    # 客户端地址
    print(request.META.get('REMOTE_ADDR'))

    # 来源页面
    print(request.META.get('HTTP_REFERER'))

    print(request.get_host())

    return render(request, 're.html', context={'data_mess': str(mess)})


def handle_response(request):
    res = HttpResponse('??')
    res.content_type = 'text/html'
    # 默认的类型

    res.status_code = 404
    # 自定义状态码

    red = render(request, '404.html', status=404)
    red.set_cookie('message', 'Hello Django!')
    # 设置cookie

    return red


def handle_rediect(request):
    '''重定向'''
    return HttpResponseRedirect('/zi/re/1111')
    # 重定向


def handle_reverse(request):
    '''反向传播'''
    return HttpResponseRedirect(reverse('App:home'))


def get_mysql(request):
    return HttpResponse('works??????????')


def set_sess(request):
    son = 'set cookies...'
    response = HttpResponse(son)
    response.status_code = 404
    # 随便设置一个状态码
    m = 'lbw123456'
    response.set_cookie('user', m)

    return response
